<template>
  <div class="app-container">
    <div class="filter-container">
      <el-select v-model="listQuery.gatewayAccountId" placeholder="网关账号" clearable filterable style="width: 140px;" class="filter-item">
        <el-option v-for="item in gatewayAccountOptions" :key="item.id" :label="item.username" :value="item.id" />
      </el-select>
      <el-select v-model="listQuery.gatewayAccountType" placeholder="网关账号类型" clearable style="width: 140px; margin-left: 10px;" class="filter-item">
        <el-option v-for="item in gatewayAccountTypeOptions" :key="item.code" :label="item.name" :value="item.code" />
      </el-select>
      <el-select v-model="listQuery.gatewayAccountStatus" placeholder="网关账号状态" clearable style="width: 140px; margin-left: 10px;" class="filter-item">
        <el-option v-for="item in gatewayAccountStatusOptions" :key="item.code" :label="item.name" :value="item.code" />
      </el-select>
      <el-button v-waves class="filter-item" style="margin-left: 10px;" type="primary" icon="el-icon-search" @click="handleFilter">
        查询
      </el-button>
      <el-button v-waves class="filter-item" style="margin-left: 10px;" type="info" icon="el-icon-refresh" @click="handleRefresh">
        重置
      </el-button>
      <el-button class="filter-item" style="margin-left: 10px;" type="info" icon="el-icon-circle-plus-outline" @click="handleAdd">
        添加
      </el-button>
    </div>

    <el-table v-loading="listLoading" :data="list" :header-cell-style="headerCellStyle" :cell-style="{padding:'0px'}" border stripe element-loading-text="拼命加载中">
      <el-table-column label="账户类型" align="center" width="110px">
        <template slot-scope="{row}">
          <span style="float: left;">{{ row.gatewayAccountType | gatewayAccountTypeFilter }}</span>
        </template>
      </el-table-column>
      <el-table-column label="账户名称" align="center" width="230px">
        <template slot-scope="{row}">
          <span style="float: left;">{{ row.gatewayAccountName }}</span>
        </template>
      </el-table-column>
      <el-table-column label="账户余额" align="center" width="230px">
        <template slot-scope="{row}">
          <router-link :to="'/manager/finance/account/'+row.id+'/edit'">
            <el-link type="primary" style="float: right;">{{ row.accountBalance }}</el-link>
          </router-link>
        </template>
      </el-table-column>
      <el-table-column label="账户状态" align="center" width="80px">
        <template slot-scope="{row}">
          <span>{{ row.gatewayAccountStatus | gatewayAccountStatusFilter }}</span>
        </template>
      </el-table-column>
      <el-table-column label="账户备注" align="center" min-width="230px">
        <template slot-scope="{row}">
          <span style="float: left;">{{ row.gatewayAccountRemark }}</span>
        </template>
      </el-table-column>
      <el-table-column label="更新时间" align="center" width="160px">
        <template slot-scope="{row}">
          <span>{{ row.updateTime }}</span>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="listQuery.pageNo" :limit.sync="listQuery.pageSize" @pagination="getList" />

    <el-dialog title="账户添加" :visible.sync="addVisible">
      <el-form ref="addForm" :rules="addRules" :model="add" label-width="80px" style="margin-left:0px;">
        <el-form-item label="网关账号" prop="gatewayAccountId">
          <el-select v-model="add.gatewayAccountId" clearable filterable style="width: 140px;">
            <el-option v-for="item in gatewayAccountOptions" :key="item.id" :label="item.username" :value="item.id" />
          </el-select>
        </el-form-item>
        <el-form-item label="账户余额" prop="accountBalance">
          <el-input v-model="add.accountBalance" type="number" step="0.01" :min="0" :max="99999999.99" style="width: 200px;" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="addVisible = false">取消</el-button>
        <el-button type="primary" @click="addConfirm()">确认</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
import { Message } from 'element-ui'
import { fetchEnum } from '@/api/system-api'
import { getFinanceAccountByPage, addFinanceAccount } from '@/api/manager/finance-api'
import waves from '@/directive/waves'
import Pagination from '@/components/Pagination'
import { toKeyValueMap } from '@/utils/index'

var gatewayAccountTypeKeyValue = {}
var gatewayAccountStatusKeyValue = {}

export default {
  name: 'ManagerFinanceAccount',
  components: { Pagination },
  directives: { waves },
  filters: {
    gatewayAccountTypeFilter(code) {
      return gatewayAccountTypeKeyValue[code]
    },
    gatewayAccountStatusFilter(code) {
      return gatewayAccountStatusKeyValue[code]
    }
  },
  data() {
    return {
      beforeRouteEnterNextIgnore: true,

      gatewayAccountOptions: [],
      gatewayAccountTypeOptions: [],
      gatewayAccountStatusOptions: [],
      listQuery: {
        pageNo: 1,
        pageSize: 20,
        gatewayAccountId: null,
        gatewayAccountType: null,
        gatewayAccountStatus: null
      },

      listLoading: true,
      list: null,
      total: 0,

      addVisible: false,
      add: {
        gatewayAccountId: null,
        accountBalance: null
      },
      addRules: {
        gatewayAccountId: [{
          required: true, message: '请输入网关账户', trigger: 'blur'
        }],
        accountBalance: [{
          required: true, message: '请输入账户余额', trigger: 'blur'
        }]
      }

    }
  },
  beforeRouteEnter(to, from, next) {
    next(vm => {
      if (!vm.beforeRouteEnterNextIgnore) {
        vm.getList()
      } else {
        vm.beforeRouteEnterNextIgnore = false
      }
    })
  },
  async created() {
    await this.initLoad()
    await this.getList()
  },
  methods: {
    async initLoad() {
      const dataDictsParam = ['GATEWAY_ACCOUNT', 'GATEWAY_ACCOUNT_TYPE', 'GATEWAY_ACCOUNT_STATUS']
      const { data } = await fetchEnum(dataDictsParam)
      for (const dataDict of data) {
        if (dataDict.code === 'GATEWAY_ACCOUNT') {
          if (dataDict.options) {
            this.gatewayAccountOptions = dataDict.options
          }
        } else if (dataDict.code === 'GATEWAY_ACCOUNT_TYPE') {
          if (dataDict.options) {
            this.gatewayAccountTypeOptions = dataDict.options
            gatewayAccountTypeKeyValue = toKeyValueMap(this.gatewayAccountTypeOptions, 'code', 'name')
          }
        } else if (dataDict.code === 'GATEWAY_ACCOUNT_STATUS') {
          if (dataDict.options) {
            this.gatewayAccountStatusOptions = dataDict.options
            gatewayAccountStatusKeyValue = toKeyValueMap(this.gatewayAccountStatusOptions, 'code', 'name')
          }
        }
      }
      this.initListQuery()
    },
    getList() {
      this.listLoading = true
      getFinanceAccountByPage(this.listQuery).then(response => {
        const { data } = response
        this.list = data.result
        this.total = data.totalCount
        setTimeout(() => {
          this.listLoading = false
        }, 1 * 1000)
      }).catch(error => {
        Message({
          message: error,
          type: 'error',
          duration: 5 * 1000
        })
        this.listLoading = false
      })
    },
    initListQuery() {
      this.listQuery = {
        pageNo: 1,
        pageSize: 20,
        gatewayAccountId: null,
        gatewayAccountType: null,
        gatewayAccountStatus: null
      }
    },

    handleFilter() {
      this.getList()
    },
    handleRefresh() {
      this.initListQuery()
      this.getList()
    },
    handleAdd() {
      this.add = {
        gatewayAccountId: null,
        accountBalance: null
      }
      this.addVisible = true
    },
    headerCellStyle({ row, rowIndex }) {
      return 'background-color: #42b983; color: #FFF;'
    },

    addConfirm() {
      this.$refs.addForm.validate((valid) => {
        if (!valid) {
          return
        }
        var data = JSON.parse(JSON.stringify(this.add))
        addFinanceAccount(data).then(response => {
          this.$notify({
            title: '成功',
            message: '添加成功',
            type: 'success',
            duration: 2000
          })
          this.addVisible = false
          this.getList()
        }).catch(error => {
          Message({
            message: error,
            type: 'error',
            duration: 5 * 1000
          })
        })
      })
    }

  }
}

</script>
